package com.prs.crm.dao.sale.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.sale.SalesContractDao;
import com.prs.crm.domain.sale.SalesContract;
import com.prs.crm.domain.sale.SalesContractDetail;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;

public class SalesContractDaoImpl extends BaseDaoImpl<SalesContract> implements
		SalesContractDao {

	@Override
	public void saveOrUpdate(SalesContract sales) {
		this.getHibernateTemplate().saveOrUpdate(sales);

	}

	@Override
	public SalesContract getSalesContract(Serializable id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(SalesContract.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public SalesContractDetail getSalesContractDetail(final Product product,
			final SalesContract salesContract) {
		return (SalesContractDetail) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(SalesContractDetail.class);
						criteria
								.add(
										Restrictions.eq("salesContract",
												salesContract)).add(
										Restrictions.eq("product", product));
						List<SalesContractDetail> result = criteria.list();
						if (result.size() > 0)
							return result.get(0);
						else
							return null;
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public double getSaleContract(final SalesContract re) {
		return (Double) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException {
				Criteria criteria = session
						.createCriteria(SalesContractDetail.class);
				criteria.add(Restrictions.eq("salesContract", re));
				List<SalesContractDetail> slist = criteria.list();
				if (slist.size() == 0) {
					return 0.00;
				} else {
					double num = 0;
					for (int i = 0; i < slist.size(); i++) {
						SalesContractDetail poObject = slist.get(i);
						num = num + poObject.getTotalPrice();
					}
					return num;
				}
			}

		});
	}

	@Override
	public void saveOrUpdateDetail(SalesContractDetail salesContractDetail) {
		this.getHibernateTemplate().saveOrUpdate(salesContractDetail);
	}

	@Override
	public List<SalesContractDetail> contractDetails(SalesContract salesContract) {
		Criteria criteria = this.getSession().createCriteria(
				SalesContractDetail.class);
		criteria.add(Restrictions.eq("salesContract", salesContract));
		return (List<SalesContractDetail>) criteria.list();
	}

	@Override
	public List<SalesContractDetail> getContractDetailsNotReceives(
			SalesContract salesContract) {
		Criteria criteria = this.getSession().createCriteria(
				SalesContractDetail.class);
		criteria.add(Restrictions.eq("salesContract", salesContract));
//		criteria.add(Restrictions.eq("salesContract", salesContract)).add(
//				Restrictions.eq("receivesNumber", 0)).add(
//				Restrictions.eq("receivesNumber", 1));
		return (List<SalesContractDetail>) criteria.list();
	}

}
